<?php

namespace App\Http\Validate\WorkOrder\Apply;

use App\Enums\WorkOrder\MissionEnum;
use App\Enums\WorkOrder\OrderEnum;
use App\Http\Validate\AbstractBaseValidate;
use App\Models\WorkOrder\WorkOrderApplyMainModel;
use Illuminate\Support\MessageBag;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class ApplyValidate extends AbstractBaseValidate
{
    public function getRules(): array
    {
        // TODO: Implement getRules() method.
        return [

        ];
    }

    public function getMessages(): array
    {
        // TODO: Implement getMessages() method.
        return [];
    }

    public function getAttributes(): array
    {
        // TODO: Implement getAttributes() method.
        return [];
    }

    public function registerValidateRules(): void
    {
        // TODO: Implement registerValidateRules() method.
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function pressNoticeValidate(array $params)
    {
        return self::getInstance()->validate(
            ["workOrderNo"],
            $params,
            ["workOrderNo" => Rule::exists("work_order_apply_main", "workOrderNo")]
        );
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function missionNoticeValidate(array $params)
    {
        return self::getInstance()->validate(
            ["workOrderNo"],
            $params,
            [
                "workOrderNo" => Rule::exists("work_order_apply_main", "workOrderNo"),
                "id"          => Rule::exists("work_order_apply_manager", "id")
                                     ->where("workOrderNo", $params["workOrderNo"])
            ]
        );
    }


    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function mainDetailValidate(array $params)
    {
        return self::getInstance()->validate(
            ["workOrderNo"],
            $params,
            ["workOrderNo" => Rule::exists("work_order_apply_main", "workOrderNo")]
        );
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function updatePriorityValidate(array $params)
    {
        return self::getInstance()->validate(
            ["workOrderNo", "priority"],
            $params,
            [
                "priority"    => [
                    "required",
                    Rule::in(array_filter(array_keys(OrderEnum::PRIORITY_TEXT)))
                ],
                "workOrderNo" => Rule::exists("work_order_apply_main", "workOrderNo")
            ]
        );
    }


    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function updateShopMobileValidate(array $params)
    {
        return self::getInstance()->validate(
            ["workOrderNo", "mobile"],
            $params,
            [
                "mobile"      => function ($attribute, $value, $fail) {
                    if (!preg_match('/^1\d{10}$/', $value)) {
                        $fail("门店手机号格式错误!");
                    }
                },
                "workOrderNo" => Rule::exists("work_order_apply_main", "workOrderNo")
            ]
        );
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function submitRemarkAndSolutionValidate(array $params)
    {
        return self::getInstance()->validate(
            ["workOrderNo", "id"],
            $params,
            [
                "workOrderNo" => "string|required",
                "id"          => Rule::exists("work_order_apply_manager", "id")
                                     ->where("workOrderNo", $params["workOrderNo"]),
                "remark"      => "string",
                "solution"    => "string"
            ]
        );
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function submitRemarkValidate(array $params)
    {
        return self::getInstance()->validate(
            ["workOrderNo", "id", "remark"],
            $params,
            [
                "workOrderNo" => Rule::exists((new WorkOrderApplyMainModel())->getTable(), "workOrderNo"),
                "id"          => Rule::exists("work_order_apply_manager", "id")
                                     ->where("workOrderNo", $params["workOrderNo"]),
                "remark"      => "string|required|min:1"
            ]
        );
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function submitSolutionValidate(array $params)
    {
        return self::getInstance()->validate(
            ["workOrderNo", "id", "solution"],
            $params,
            [
                "workOrderNo" => Rule::exists((new WorkOrderApplyMainModel())->getTable(), "workOrderNo"),
                "id"          => Rule::exists("work_order_apply_manager", "id")
                                     ->where("workOrderNo", $params["workOrderNo"]),
                "solution"    => "string|required|min:1"
            ]
        );
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function submitValidate(array $params)
    {
        return self::getInstance()->validate(
            ["workOrderNo", "id"],
            $params,
            [
                "workOrderNo" => Rule::exists((new WorkOrderApplyMainModel())->getTable(), "workOrderNo"),
                "id"          => Rule::exists("work_order_apply_manager", "id")
                                     ->where("workOrderNo", $params["workOrderNo"])
            ]
        );
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function closeWorkOrderValidate(array $params)
    {
        return self::getInstance()->validate(
            ["workOrderNo"],
            $params,
            ["workOrderNo" => Rule::exists("work_order_apply_main", "workOrderNo")]
        );
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function closeWorkOrderV2Validate(array $params)
    {
        return self::getInstance()->validate(
            ["workOrderNo", "reason"],
            $params,
            [
                "workOrderNo" => Rule::exists("work_order_apply_main", "workOrderNo")->where(function ($query) {
                    $query->where("step", "<", MissionEnum::STEP_99);
                }),
                "reason"      => "required|string|min:1"
            ]
        );
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function updateContentValidate(array $params)
    {
        return self::getInstance()->validate(
            ["workOrderNo", "formContent"],
            $params,
            [
                "workOrderNo" => Rule::exists("work_order_apply_main", "workOrderNo"),
                "formContent" => "required|array"
            ]
        );
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function circulationValidate(array $params)
    {
        return self::getInstance()->validate(
            ["workOrderNo", "managerId"],
            $params,
            [
                "workOrderNo" => Rule::exists("work_order_apply_main", "workOrderNo"),
                "managerId"   => Rule::exists("work_order_user_role", "userId")
            ]
        );
    }
}
